package com.zadetk.helpyou.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.bean.ServiceItemBean;
import com.zadetk.helpyou.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * 6-2发布岗位页面
 */
public class MyJobActivity extends AbsActivity implements View.OnClickListener {
    private TextView tvPublish;
    private RecyclerView recyclerView;
    private PublishJobAdapter serviceAdapter;
    private List<ServiceItemBean> data = new LinkedList<>();

    @Override
    public int setView() {
        return R.layout.activity_my_job;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
        requestData();
    }

    private void requestData() {
        ServiceItemBean itemBean = new ServiceItemBean();
        data.add(itemBean);
        data.add(itemBean);
        data.add(itemBean);
        data.add(itemBean);
        serviceAdapter.notifyDataSetChanged();
    }

    private void initLogic() {
        serviceAdapter = new PublishJobAdapter(R.layout.publish_service_item, data);
        serviceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_delete:
                        deleteRequest();
                        break;
                    case R.id.iv_edit:
                        startActivity(new Intent(MyJobActivity.this, PublishJobActivity.class));
                        break;
                }
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(serviceAdapter);
        tvPublish.setOnClickListener(this);
    }

    private void deleteRequest() {
        ToastUtil.showToast("删除条目请求！");
    }

    private void initViews() {
        tvPublish = findViewById(R.id.tv_btn_publish);
        recyclerView = findViewById(R.id.recycleview);
        setTopTitle("我发布的岗位");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_publish:
                startActivity(new Intent(this, PublishJobActivity.class));
                break;
        }
    }

    private class PublishJobAdapter extends BaseQuickAdapter<ServiceItemBean, BaseViewHolder> {
        public PublishJobAdapter(int layoutResId, @Nullable List<ServiceItemBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ServiceItemBean item) {
            helper.addOnClickListener(R.id.iv_delete);
            helper.addOnClickListener(R.id.iv_edit);
        }
    }
}
